<template>
  <view class="custom-tabbar">
    <view class="tabbar-container">
      <!-- 首页 -->
      <view 
        class="tabbar-item" 
        :class="{ 'active': currentPath === '/pages/index/index' }"
        @click="switchTab('/pages/index/index')"
      >
        <image 
          :src="currentPath === '/pages/index/index' ? '/static/tabbar/home_active.png' : '/static/tabbar/home.png'" 
          class="tabbar-icon"
          mode="aspectFit"
        ></image>
        <text class="tabbar-text">首页</text>
      </view>

      <!-- 搭子 -->
      <view 
        class="tabbar-item" 
        :class="{ 'active': currentPath === '/pages/index/buddy' }"
        @click="switchTab('/pages/index/buddy')"
      >
        <image 
          :src="currentPath === '/pages/index/buddy' ? '/static/tabbar/buddy_active.png' : '/static/tabbar/buddy.png'" 
          class="tabbar-icon"
          mode="aspectFit"
        ></image>
        <text class="tabbar-text">搭子</text>
      </view>

      <!-- 发布按钮（中间） -->
      <view 
        class="tabbar-item tabbar-publish" 
        @click.stop="handlePublish"
        @touchstart.stop="handlePublish"
      >
        <image 
          src="/static/tabbar/plush.png" 
          class="tabbar-icon"
          mode="aspectFit"
        ></image>
        <text class="tabbar-text">发布</text>
      </view>

      <!-- 订单 -->
      <view 
        class="tabbar-item" 
        :class="{ 'active': currentPath === '/pages/order/index' }"
        @click="switchTab('/pages/order/index')"
      >
        <image 
          :src="currentPath === '/pages/order/index' ? '/static/tabbar/order_active.png' : '/static/tabbar/order.png'" 
          class="tabbar-icon"
          mode="aspectFit"
        ></image>
        <text class="tabbar-text">订单</text>
      </view>

      <!-- 我的 -->
      <view 
        class="tabbar-item" 
        :class="{ 'active': currentPath === '/pages/index/user' }"
        @click="switchTab('/pages/index/user')"
      >
        <image 
          :src="currentPath === '/pages/index/user' ? '/static/tabbar/mine_active.png' : '/static/tabbar/mine.png'" 
          class="tabbar-icon"
          mode="aspectFit"
        ></image>
        <text class="tabbar-text">我的</text>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { onShow } from '@dcloudio/uni-app';

const currentPath = ref('');

// 获取当前页面路径
const getCurrentPath = () => {
  const pages = getCurrentPages();
  if (pages.length > 0) {
    const currentPage = pages[pages.length - 1];
    return '/' + currentPage.route;
  }
  return '';
};

// 更新当前路径
const updateCurrentPath = () => {
  currentPath.value = getCurrentPath();
};

// 切换tab
const switchTab = (url) => {
  if (currentPath.value === url) {
    return; // 如果已经在当前页面，不执行跳转
  }
  
  // 使用reLaunch来切换tabBar页面（因为使用了custom: true）
  uni.reLaunch({
    url: url
  });
};

// 处理发布按钮点击
const handlePublish = (e) => {
  console.log('发布按钮被点击', e);
  uni.$emit('showPublishModal');
};

onMounted(() => {
  updateCurrentPath();
});

onShow(() => {
  updateCurrentPath();
});
</script>

<style lang="scss" scoped>
.custom-tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 9999;
  background-color: #FFFFFF;
  border-top: 1rpx solid #e5e5e5;
  padding-bottom: env(safe-area-inset-bottom);
  padding-bottom: constant(safe-area-inset-bottom);
  box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  pointer-events: auto;
}

.tabbar-container {
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 98rpx;
  padding: 0 20rpx;
  position: relative;
  z-index: 10000;
}

.tabbar-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  position: relative;
  pointer-events: auto;
  cursor: pointer;

  .tabbar-icon {
    width: 44rpx;
    height: 44rpx;
    margin-bottom: 4rpx;
    pointer-events: none;
  }

  .tabbar-text {
    font-size: 20rpx;
    color: #8a8f98;
    line-height: 1;
    pointer-events: none;
  }

  &.active {
    .tabbar-text {
      color: #111111;
    }
  }

  &.tabbar-publish {
    z-index: 10001;
  }
}

</style>

